#include "KEY.h"

uint8_t Key_row[1]={0xff}; 

/***
 *函数名：KEY_ROW_SCAN
 *功  能：按键行扫描
 *返回值：1~4，对应1~4行按键位置
 */
char KEY_ROW_SCAN(void)
{
    //读出行扫描状态
		Key_row[0] = HAL_GPIO_ReadPin(GPIOB,KEY_ROW0_Pin)<<3;
    Key_row[0] = Key_row[0] | (HAL_GPIO_ReadPin(GPIOA,KEY_ROW1_Pin)<<2);
    Key_row[0] = Key_row[0] | (HAL_GPIO_ReadPin(GPIOA,KEY_ROW2_Pin)<<1);
//    Key_row[0] = Key_row[0] | (HAL_GPIO_ReadPin(GPIOE,KEY_row3_Pin));

    
    if(Key_row[0] != 0x0f)         //行扫描有变化，判断该列有按键按下
    {
      HAL_Delay(10);                    //消抖
      if(Key_row[0] != 0x0f)
        {   
//                printf("Key_Row_DATA = 0x%x\r\n",Key_row[0]);
                switch(Key_row[0])
                {
                    case 0x06:         //0110 判断为该列第1行的按键按下
                        return 1;
                    case 0x0a:         //1010 判断为该列第2行的按键按下
                        return 2;
                    case 0x0c:         //1100 判断为该列第3行的按键按下
                        return 3;
//                    case 0x0e:         //1110 判断为该列第4行的按键按下
//                        return 4;
                    default :
                        return 0;
                }
        }
        else return 0;
    }
    else return 0;
}



/***
 *函数名：KEY_SCAN
 *功  能：4*4按键扫描
 *返回值：0~16，对应16个按键
 */
char KEY_SCAN(void)
{    
    char Key_Num=0;       //1-16对应的按键数
    char key_row_num=0;        //行扫描结果记录
    
    KEY_CLO0_OUT_LOW;        
    if( (key_row_num=KEY_ROW_SCAN()) != 0 )
    { 
        while(KEY_ROW_SCAN() != 0);  //消抖
        Key_Num = 0 + key_row_num;
    }
    KEY_CLO0_OUT_HIGH;
    
    KEY_CLO1_OUT_LOW;        
    if( (key_row_num=KEY_ROW_SCAN()) != 0 )
    { 
        while(KEY_ROW_SCAN() != 0);
        Key_Num = 3 + key_row_num;
    }
    KEY_CLO1_OUT_HIGH;
    
    KEY_CLO2_OUT_LOW;    
    if( (key_row_num=KEY_ROW_SCAN()) != 0 )
    { 
        while(KEY_ROW_SCAN() != 0);
				Key_Num = 6 + key_row_num;
    }
    KEY_CLO2_OUT_HIGH;
    
    KEY_CLO3_OUT_LOW;    
    if( (key_row_num=KEY_ROW_SCAN()) != 0 )
    {
        while(KEY_ROW_SCAN() != 0);
        Key_Num = 9 + key_row_num;
    }
    KEY_CLO3_OUT_HIGH;
    
    return Key_Num;
}


/***
 *函数名：KEY_ROW_SCAN
 *功  能：执行按下按键后的操作
 *返回值：无
 */
void HW_KEY_FUNCTION(void)
{
    char key_confirm;
    key_confirm = KEY_SCAN();
    if( 0 < key_confirm  && key_confirm < 10 )
    {
        printf("Key_NUM = %d \r\n",key_confirm); //按下1-16个按键的操作
        printf("= = = = = = = = = = = \r\n");
    }
		else if(key_confirm==10)
		{
				printf("Key_NUM = * \r\n"); //按下1-16个按键的操作
        printf("= = = = = = = = = = = \r\n");
		}
		else if(key_confirm==11)
		{
				printf("Key_NUM = %d \r\n",0); //按下1-16个按键的操作
        printf("= = = = = = = = = = = \r\n");
		}
		else if(key_confirm==12)
		{
				printf("Key_NUM = # \r\n"); //按下1-16个按键的操作
        printf("= = = = = = = = = = = \r\n");
		}
}





